import { get } from 'lodash';
import {WeaLocaleProvider} from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export default class QuickTableFieldCellType extends GC.Spread.Sheets.CellTypes.Text {
    typeName = "QuickTableFieldCellType";
    constructor(quickTable, fieldKey) {
        super();
        this.quickTable = quickTable;
        this.fieldKey = fieldKey;
    }
    format(value, format, formattedData, context) {
        const field = get(this.quickTable, this.fieldKey);
        if (field) {
            const [htmltype, type] = field.fieldType;
            let fieldTypeName = '';
            if (htmltype === 'input') {
                fieldTypeName = { 'text': getLabel('502305','文本'), 'int': getLabel('502306','整数'), 'float': getLabel('502307','浮点数') }[type];
            } else if (htmltype === 'browser') {
                fieldTypeName = type ? type.valueSpan : '';
            } else if (htmltype === 'check') {
                fieldTypeName = getLabel('502309','Check框');
            } else if (htmltype === 'select') {
                fieldTypeName = getLabel('502310','下拉框')
            }
            return `${field.label}(${field.fieldName})[${fieldTypeName}]`;
        }
        return value;
    }
    setFieldType = (fieldType) => {
        const field = get(this.quickTable, this.fieldKey);
        if (field) {
            const [htmltype, type, options] = field.fieldType || [];
            if (htmltype === 'select') {
                fieldType[2] = options;
            }
            field.fieldType = fieldType;
        }
    }
    get backgroundImage() {
        const field = get(this.quickTable, this.fieldKey);
        if (field) {
            const [htmltype, type] = field.fieldType;
            if (htmltype === 'input') {
                return '/formmode/exceldesign/image/controls/text2_wev8.png';
            } if (htmltype === 'browser') {
                const value = type ? type.value : '';
                if (value === '19') {
                    return '/formmode/exceldesign/image/controls/time2_wev8.png';
                } else if (value === '2' || value === '290') {
                    return '/formmode/exceldesign/image/controls/date2_wev8.png';
                } else {
                    return '/formmode/exceldesign/image/controls/browser2_wev8.png';
                }
            } else if (htmltype === 'check') {
                return '/formmode/exceldesign/image/controls/checkbox2_wev8.png';
            } else if (htmltype === 'select') {
                return '/formmode/exceldesign/image/controls/textarea2_wev8.png';
            }
        }
        return '';
    }
    toJSON() {
        const field = get(this.quickTable, this.fieldKey);
        return {
            detailIndex: field.detailIndex,
            fieldName: field.fieldName,
            fieldType: field.fieldType,
            label: field.label,
            typeName: 'FieldCellType'
        }
    }
}